package com.liuceng.zen.web.auth;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WechatLoginMatcher extends SimpleCredentialsMatcher {
  private final static Logger LOG = LoggerFactory.getLogger(WechatLoginMatcher.class);

    private String allowReferer;

    public WechatLoginMatcher(String allowReferer) {
        this.allowReferer = allowReferer;
    }

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken atoken = (UsernamePasswordToken) token;
        String password = String.valueOf(atoken.getPassword());
        //
        if (atoken.getUsername() != null) {
            if (!password.contains(allowReferer)) {
                LOG.error("referer not allow " + password);
                return false;
            }
            return true;
        }
        return false;
    }

    public String getAllowReferer() {
        return allowReferer;
    }

    public void setAllowReferer(String allowReferer) {
        this.allowReferer = allowReferer;
    }
}
